﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.VisualStudio.Shell;

namespace Tcvt.Packages
{
    public partial class WorkItemContainerUserControl : UserControl
    {
        private WorkItem _workItem;

        public WorkItemContainerUserControl()
        {
            InitializeComponent();

            ResizeControls();
        }

        private void ResizeControls()
        {
            this.workItemInformationBar.ProgressBarValue = -1;
            this.workItemFormControl.ReadOnly = false;
            //this.workItemFormControl.ShowAllFieldErrors("Field Error", -1);
        }

        public WorkItem WorkItem
        {
            get
            {
                return _workItem;
            }
            set
            {
                _workItem = value;

                try
                {
                    int id = _workItem.Id;
                    string title = _workItem.Title;

                    this.workItemInformationBar.Text = string.Format("Work Item {0} : {1}", id, title);
                    this.workItemFormControl.Item = _workItem;
                }
                catch (Exception ex)
                {
                    Utils.LogMessage(string.Format("WorkItemContainerUserControl:WorkItem_set: Error setting work item: Message: {0}", ex.Message), LogType.Error);
                    Utils.LogMessage(string.Format("WorkItemContainerUserControl:WorkItem_set: Error setting work item: {0}", ex.StackTrace), LogType.Error);
                    Utils.ShowErrorMessageBox(string.Format("Error setting work item:\r\n\r\n{0}", ex.Message));
                }
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.WorkItem.SyncToLatest();
            }
            catch (Exception ex)
            {
                Utils.LogMessage(string.Format("WorkItemContainerUserControl:refreshToolStripMenuItem_Click: Error refreshing work item: Message: {0}", ex.Message), LogType.Error);
                Utils.LogMessage(string.Format("WorkItemContainerUserControl:refreshToolStripMenuItem_Click: Error refreshing work item: {0}", ex.StackTrace), LogType.Error);
                Utils.ShowErrorMessageBox(string.Format("Error refreshing work item:\r\n\r\n{0}", ex.Message));
            }
        }

        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.WorkItem.Save();
            }
            catch (Exception ex)
            {
                Utils.LogMessage(string.Format("WorkItemContainerUserControl:updateToolStripMenuItem_Click: Error updating work item: Message: {0}", ex.Message), LogType.Error);
                Utils.LogMessage(string.Format("WorkItemContainerUserControl:updateToolStripMenuItem_Click: Error updating work item: {0}", ex.StackTrace), LogType.Error);
                Utils.ShowErrorMessageBox(string.Format("Error updating work item:\r\n\r\n{0}", ex.Message));
            }
        }
    }
}
